//
//  ZXYAlertView.m
//  AlertViewDemo
//
//  Created by Apple on 16/9/8.
//  Copyright © 2016年 深圳中业兴融互联网金融服务有限公司. All rights reserved.
//

#define KWidth [[UIScreen mainScreen]bounds].size.width
#define KHeight [[UIScreen mainScreen]bounds].size.height
#define kAlertWidth 300

#import "ZXYAlertView.h"

@interface ZXYAlertView ()

@property (nonatomic, strong) UIView    *bgView;
@property (nonatomic, strong) UILabel   *titleLabel;

@property (nonatomic, strong) UIView    *grayHLine;
@property (nonatomic, strong) UIView    *grayVLine;
@property (nonatomic, strong) UIButton  *button;


@end

@implementation ZXYAlertView
+ (instancetype)alertViewDefault {
    return [[self alloc]initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, KWidth, KHeight);
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        
        //背景视图(300,150)
        self.bgView = [[UIView alloc]init];
        self.bgView.center = self.center;
        self.bgView.bounds = CGRectMake(0, 0, kAlertWidth, 150);
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.cornerRadius = 10;
        self.bgView.layer.masksToBounds = YES;
        [self addSubview:self.bgView];
        
        //标题
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kAlertWidth, 50)];
        self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:17];
        self.titleLabel.textColor = [UIColor colorWithRed:3/255.0 green:3/255.0 blue:3/255.0 alpha:1];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.text= @"提示";
        self.titleLabel.backgroundColor = Colorbackground;
        [self.bgView addSubview:self.titleLabel];
        

        
        //水平灰线
        self.grayHLine = [[UIView alloc]init];
        self.grayHLine.bounds = CGRectMake(0, 0, kAlertWidth, 0.5);
        self.grayHLine.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1];
        [self.bgView addSubview:self.grayHLine];

        //垂直灰线
        self.grayVLine = [[UIView alloc]init];
        self.grayVLine.bounds = CGRectMake(0, 0, 0.5, 44);
        self.grayVLine.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1];
        
        
    }
    return self;
}

- (void)show {
    
    
    if (self.title) {
        self.titleLabel.text= self.title;
    }
    

    
    self.bgView.bounds = CGRectMake(0, 0, kAlertWidth, 50);
    self.grayHLine.center = CGPointMake(kAlertWidth/2, 50);
    
    
   
        
        for (int i=0; i<self.buttonArray.count; i++) {
            
            self.button = [UIButton buttonWithType:UIButtonTypeCustom];
            self.button.frame = CGRectMake(0, _titleLabel.frame.size.height+50*i, kAlertWidth, 44);
            [self.bgView addSubview:self.button];
            [self.button setTitle:self.buttonArray[i] forState:UIControlStateNormal];
            self.button.tag = i;
            [self.button setTitleColor:[UIColor colorWithRed:61/255.0 green:61/255.0 blue:61/255.0 alpha:1] forState:UIControlStateNormal];
            [self.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            self.bgView.bounds = CGRectMake(0, 0, kAlertWidth, 50*(self.buttonArray.count+1));
            
            self.grayHLine = [[UIView alloc]init];
            self.grayHLine.frame = CGRectMake(0, self.button.frame.origin.y+44, kAlertWidth, 0.5);
            self.grayHLine.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1];
            [self.bgView addSubview:self.grayHLine];
            if (i==self.buttonArray.count-1) {
                self.grayHLine.hidden = YES;
            }
        
    }
}

- (void)buttonClick:(UIButton *)button {
    if (self.delegate)
    {
        [self.delegate alertView:self clickedCustomButtonAtIndex:button.tag];
    }
    [self removeFromSuperview];
}



@end
